package cn.xshi.monitor.service.impl;

import cn.xshi.common.base.BaseResult;
import cn.xshi.common.base.BaseService;
import cn.xshi.common.idgeneration.SnowflakeIdWorker;
import cn.xshi.common.util.ExceptionUtil;
import cn.xshi.common.util.StringUtil;
import cn.xshi.monitor.dao.IotVideoGroupDao;
import cn.xshi.monitor.dao.IotVideoGroupRightDao;
import cn.xshi.monitor.form.IotVideoGroupRightForm;
import cn.xshi.monitor.model.IotVideoGroup;
import cn.xshi.monitor.model.IotVideoGroupRight;
import cn.xshi.monitor.service.IotVideoGroupService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
/**
 * @Desc 相机组
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@Service
public class IotVideoGroupServiceImpl extends BaseService implements IotVideoGroupService {

    @Resource
    IotVideoGroupDao iotVideoGroupDao;

    @Resource
    IotVideoGroupRightDao iotVideoGroupRightDao;

    @Resource
    SnowflakeIdWorker snowflakeIdWorker;

    public static final String VG_CODE = "VG";

    /**
     * 分页
     * @param condition
     * @return
     */
    public List<IotVideoGroup> getIotVideoGroupByCondition(Map<String, Object> condition){
        return iotVideoGroupDao.getIotVideoGroupByCondition(condition);
    }
    /**
     * 查询对象
     * @param id
     * @return
     */
    public IotVideoGroup getIotVideoGroupById(String id){
        return iotVideoGroupDao.getIotVideoGroupById(id);
    }
    /**
     * 添加
     * @param iotVideoGroup
     * @return
     */
    public int addIotVideoGroup(IotVideoGroup iotVideoGroup){
        int i = 0;
        try{
            iotVideoGroup.setCode(VG_CODE+snowflakeIdWorker.nextId());
            iotVideoGroup.setCreateId(getCurrentAccountId());
            iotVideoGroup.setId(toUUID());
            iotVideoGroup.setCreateTime(getDate());
            i = iotVideoGroupDao.addIotVideoGroup(iotVideoGroup);
        } catch (Exception e) {
            throw new ExceptionUtil(e.getMessage(),e.getCause());
        }
        return i;
    }
    /**
     * 修改
     * @param iotVideoGroup
     * @return
     */
    public int updateIotVideoGroup(IotVideoGroup iotVideoGroup){
        int i = 0;
        try{
            iotVideoGroup.setUpdateTime(getDate());
            iotVideoGroup.setUpdateId(getCurrentAccountId());
            i = iotVideoGroupDao.updateIotVideoGroup(iotVideoGroup);
        } catch (Exception e) {
            throw new ExceptionUtil(e.getMessage(),e.getCause());
        }
        return i;
    }
    /**
     * 删除
     * @param condition
     * @return
     */
    public int delIotVideoGroup(Map<String, Object> condition){
        int i = 0;
        try{
            i = iotVideoGroupDao.delIotVideoGroup(condition);
        } catch (Exception e) {
            throw new ExceptionUtil(e.getMessage(),e.getCause());
        }
        return i;
    }

    /**
     * 添加组相机
     * @param iotVideoGroupRightForm
     * @return
     */
    public BaseResult addRight(IotVideoGroupRightForm iotVideoGroupRightForm){
        if(StringUtil.isEmpty(iotVideoGroupRightForm.getVideoGroupId())){
            throw new ExceptionUtil("未能获取到组id");
        }
        String deviceId = iotVideoGroupRightForm.getDeviceId();
        if(!StringUtil.isEmpty(deviceId)){
            String [] ids = deviceId.split(",");
            for(String id : ids){
                IotVideoGroupRight iotVideoGroupRight = new IotVideoGroupRight();
                iotVideoGroupRight.setDeviceId(id);
                iotVideoGroupRight.setId(toUUID());
                iotVideoGroupRight.setCreateId(getCurrentAccountId());
                iotVideoGroupRight.setCreateTime(getDate());
                iotVideoGroupRight.setVideoGroupId(iotVideoGroupRightForm.getVideoGroupId());
                iotVideoGroupRightDao.addIotVideoGroupRight(iotVideoGroupRight);
            }
        }
        return BaseResult.success();
    }
}
